package com.junsi.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javassist.expr.NewArray;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.Client;
import com.junsi.bean.Communal;
import com.junsi.bean.Department;
import com.junsi.bean.Information;
import com.junsi.bean.Rank;
import com.junsi.bean.Record;
import com.junsi.bean.Sale;
import com.junsi.bean.SaleTrack;
import com.junsi.bean.Salestage;
import com.junsi.bean.Sources;
import com.junsi.bean.Staff;
import com.junsi.bean.TradeManages;
import com.junsi.service.ClientService;
import com.junsi.service.CommunalService;
import com.junsi.service.DepartmentSerivce;
import com.junsi.service.InformationService;
import com.junsi.service.RankService;
import com.junsi.service.RecordService;
import com.junsi.service.SaleService;
import com.junsi.service.SaleTrackService;
import com.junsi.service.SalestageService;
import com.junsi.service.SourcesService;
import com.junsi.service.StaffSerivce;
import com.junsi.service.TradeManageService;
import com.junsi.service.impl.RankServiceImpl;
import com.junsi.util.NewClientUtil;
import com.junsi.util.PageUtil;
import com.junsi.util.SaleTrackUtil;
import com.junsi.util.SaleUtil;

/**
 * 销售客户跟进线索控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/SaleTrack")
public class SaleTrackController {
 
   @Autowired
   private SaleTrackService saleTrackService ;
   @Autowired
   private SaleService saleService;
   
   @Autowired
   private StaffSerivce staffSerivce;
   
   @Autowired
   private ClientService clientService;
   
   @Autowired
   private RecordService recordService;
   
   @Autowired
   private RankService rankService;
   
   @Autowired
   private ClientController clientController;
   
   @Autowired
   private TradeManageService tradeManageService;
   
   @Autowired
   private DepartmentSerivce departmentSerivce;
   
   @Autowired
	private InformationService informationService;
	
	@Autowired
	private CommunalService communalService;
   
   Logger logger=Logger.getRootLogger();
	
	/**
	 * 销售跟进新增
	 */
 	@RequestMapping("/insertSaleTrack")
 	@ResponseBody
 	public List<SaleTrackUtil> insertSaleTrack(HttpServletResponse response, SaleTrack saleTrack,PageUtil page){
 		page.setPageSize(100);
		page.setPageNum(1);
 		List<SaleTrack> saleTracks = null;
 		List<SaleTrackUtil> saleTrackUtils = new ArrayList<>();
 		if(saleTrack!=null){
 			saleTrack.setSaletrackDate(new Date());
 			    saleTrackService.save(saleTrack);
 				Integer count = saleTrackService.findCount();
 				if(count != null){
 					page.setRowCount(count);
 					 saleTracks = saleTrackService.findBysaleId(saleTrack.getSaleId(), page.getEndIndex(), page.getBeginIndex());
 					if(saleTracks!=null){
 					for (SaleTrack saleTrack1 : saleTracks) {
 						SaleTrackUtil saleTrackUtilsss = new SaleTrackUtil();
 						saleTrackUtilsss.setSaleTrack(saleTrack1);
 						saleTrackUtils.add(saleTrackUtilsss);
 					}
 						
 				int i = 0;
 				for (SaleTrackUtil saleTrackUtil2 : saleTrackUtils) {
 					if(saleTrackUtil2.getSaleTrack().getRecordId()!=null){
 						Record record=recordService.findById(saleTrackUtil2.getSaleTrack().getRecordId());
 						if(record!=null){
 							saleTrackUtils.get(i).setRecord(record.getRecordName());
 						}
 					}
 					 if(saleTrackUtil2.getSaleTrack().getStaffId()!=null){
 						 Staff staff1=staffSerivce.findById(saleTrackUtil2.getSaleTrack().getStaffId());
 						 if(staff1!=null){
 							 saleTrackUtils.get(i).setStaff(staff1.getStaff_name()); 
 						 }
 					 }
 					if(saleTrackUtil2.getSaleTrack().getSaletrackDate()!=null){
 						String sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(saleTrackUtil2.getSaleTrack().getSaletrackDate());
 						if(sdf!=null){
 							saleTrackUtils.get(i).setDate(sdf);
 						}
 					}
 					 
 					if(saleTrackUtil2.getSaleTrack().getClientId()!=null){
	 						Client client=clientService.findById(saleTrackUtil2.getSaleTrack().getClientId());
	 						if(client!=null){
	 							saleTrackUtils.get(i).setClient(client.getClient_name());
	 						}
	 					}
	 					if(saleTrackUtil2.getSaleTrack().getSaleId()!=null){
	 						Sale sale=saleService.findByID(saleTrackUtil2.getSaleTrack().getSaleId());
	 						if(sale!=null){
	 							saleTrackUtils.get(i).setSale(sale.getSaleOpportunityName());
	 						}
	 					}
 					i++;
 				}
 				
 					}
 				}
 		} 
		return saleTrackUtils;	
 		
 	}
	
	/**
	 * 客户跟进新增
	 */
 	@RequestMapping("/insertclient")
 	@ResponseBody
 	public List<SaleTrackUtil> insertclient(HttpServletResponse response, SaleTrack saleTrack,PageUtil page){
 		page.setPageSize(100);
		page.setPageNum(1);
 		List<SaleTrack> saleTracks = null;
 		List<SaleTrackUtil> saleTrackUtils = new ArrayList<>();
 		if(saleTrack!=null){
 			saleTrack.setSaletrackDate(new Date());
 			    saleTrackService.save(saleTrack);
 				Integer count = saleTrackService.findCount();
 				if(count != null){
 					page.setRowCount(count);
 					 saleTracks = saleTrackService.findclientId(saleTrack.getClientId(), page.getEndIndex(), page.getBeginIndex());
 					if(saleTrack!=null){
 					for (SaleTrack saleTrack1 : saleTracks) {
 						SaleTrackUtil saleTrackUtilsss = new SaleTrackUtil();
 						saleTrackUtilsss.setSaleTrack(saleTrack1);
 						saleTrackUtils.add(saleTrackUtilsss);
 					}
 						
 				int i = 0;
 				for (SaleTrackUtil saleTrackUtil2 : saleTrackUtils) {
 					if(saleTrackUtil2.getSaleTrack().getRecordId()!=null){
 						Record record=recordService.findById(saleTrackUtil2.getSaleTrack().getRecordId());
 						if(record!=null){
 							saleTrackUtils.get(i).setRecord(record.getRecordName());
 						}
 					}
 					 if(saleTrackUtil2.getSaleTrack().getStaffId()!=null){
 						 Staff staff1=staffSerivce.findById(saleTrackUtil2.getSaleTrack().getStaffId());
 						 if(staff1!=null){
 							 saleTrackUtils.get(i).setStaff(staff1.getStaff_name()); 
 						 }
 					 }
 					if(saleTrackUtil2.getSaleTrack().getSaletrackDate()!=null){
 						String sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(saleTrackUtil2.getSaleTrack().getSaletrackDate());
 						if(sdf!=null){
 							saleTrackUtils.get(i).setDate(sdf);
 						}
 					}
 					if(saleTrackUtil2.getSaleTrack().getClientId()!=null){
	 						Client client=clientService.findById(saleTrackUtil2.getSaleTrack().getClientId());
	 						if(client!=null){
	 							saleTrackUtils.get(i).setClient(client.getClient_name());
	 						}
	 					}
	 					if(saleTrackUtil2.getSaleTrack().getSaleId()!=null){
	 						Sale sale=saleService.findByID(saleTrackUtil2.getSaleTrack().getSaleId());
	 						if(sale!=null){
	 							saleTrackUtils.get(i).setSale(sale.getSaleOpportunityName());
	 						}
	 					}
 					
 					i++;
 				}	
 					}
 				}
 		}
		return saleTrackUtils;	
 		
 	}
 	
 	/**
	 * 销售跟进编辑,还没用
	 */
 	@RequestMapping("/updateSaletrack")
 	public void updateSale(HttpServletResponse response, SaleTrack saleTrack){
 		Boolean result=saleTrackService.update(saleTrack);
 		String s = null;
		if (result) {
			s = "{'errorMsg':'true'}";
		} else {
			s = "{'errorMsg':'false'}";
		}
		try {
			response.getWriter().write(s);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			logger.error("传输更新失败");
			e.printStackTrace();
		}
 	}

 	/**
	 * 客户跟踪线索删除
	 */
	@RequestMapping("/deleteclient")
	@ResponseBody
	public List<SaleTrackUtil> deleteclient(HttpServletResponse response, SaleTrack saleTrack,PageUtil page) {
		page.setPageSize(100);
		page.setPageNum(1);
 		List<SaleTrack> saleTracks = null;
 		List<SaleTrackUtil> saleTrackUtils = new ArrayList<>();
 		if(saleTrack!=null){
 			   Boolean result=saleTrackService.deleteById(saleTrack.getSaletrackId());
 				if (result) {
 					Integer count = saleTrackService.findCount();
 	 				if(count != null){
 	 					page.setRowCount(count);
 	 					 saleTracks = saleTrackService.findclientId(saleTrack.getClientId(), page.getEndIndex(), page.getBeginIndex());
 	 					if(saleTrack!=null){
 	 					for (SaleTrack saleTrack1 : saleTracks) {
 	 						SaleTrackUtil saleTrackUtilsss = new SaleTrackUtil();
 	 						saleTrackUtilsss.setSaleTrack(saleTrack1);
 	 						saleTrackUtils.add(saleTrackUtilsss);
 	 					}
 	 						
 	 				int i = 0;
 	 				for (SaleTrackUtil saleTrackUtil2 : saleTrackUtils) {
 	 					if(saleTrackUtil2.getSaleTrack().getRecordId()!=null){
 	 						Record record=recordService.findById(saleTrackUtil2.getSaleTrack().getRecordId());
 	 						if(record!=null){
 	 							saleTrackUtils.get(i).setRecord(record.getRecordName());
 	 						}
 	 					}
 	 					 if(saleTrackUtil2.getSaleTrack().getStaffId()!=null){
 	 						 Staff staff1=staffSerivce.findById(saleTrackUtil2.getSaleTrack().getStaffId());
 	 						 if(staff1!=null){
 	 							 saleTrackUtils.get(i).setStaff(staff1.getStaff_name()); 
 	 						 }
 	 					 }
 	 					if(saleTrackUtil2.getSaleTrack().getSaletrackDate()!=null){
 	 						String sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(saleTrackUtil2.getSaleTrack().getSaletrackDate());
 	 						if(sdf!=null){
 	 							saleTrackUtils.get(i).setDate(sdf);
 	 						}
 	 					}
 	 					if(saleTrackUtil2.getSaleTrack().getClientId()!=null){
 	 						Client client=clientService.findById(saleTrackUtil2.getSaleTrack().getClientId());
 	 						if(client!=null){
 	 							saleTrackUtils.get(i).setClient(client.getClient_name());
 	 						}
 	 					}
 	 					if(saleTrackUtil2.getSaleTrack().getSaleId()!=null){
 	 						Sale sale=saleService.findByID(saleTrackUtil2.getSaleTrack().getSaleId());
 	 						if(sale!=null){
 	 							saleTrackUtils.get(i).setSale(sale.getSaleOpportunityName());
 	 						}
 	 					}
 	 					i++;
 	 				}	
 	 					}
 	 				}
				}
 			   
 		}
		return saleTrackUtils;
	}

	/**
	 * 销售机会线索删除
	 */
	@RequestMapping("/deleteSaletrack")
	@ResponseBody
	public List<SaleTrackUtil> deleteSaletrack(HttpServletResponse response, SaleTrack saleTrack,PageUtil page) {
		page.setPageSize(100);
		page.setPageNum(1);
 		List<SaleTrack> saleTracks = null;
 		List<SaleTrackUtil> saleTrackUtils = new ArrayList<>();
 		if(saleTrack!=null){
 			   Boolean result=saleTrackService.deleteById(saleTrack.getSaletrackId());
 				if (result) {
 					Integer count = saleTrackService.findCount();
 	 				if(count != null){
 	 					page.setRowCount(count);
 	 					 saleTracks = saleTrackService.findBysaleId(saleTrack.getSaleId(), page.getEndIndex(), page.getBeginIndex());
 	 					if(saleTrack!=null){
 	 					for (SaleTrack saleTrack1 : saleTracks) {
 	 						SaleTrackUtil saleTrackUtilsss = new SaleTrackUtil();
 	 						saleTrackUtilsss.setSaleTrack(saleTrack1);
 	 						saleTrackUtils.add(saleTrackUtilsss);
 	 					}
 	 						
 	 				int i = 0;
 	 				for (SaleTrackUtil saleTrackUtil2 : saleTrackUtils) {
 	 					if(saleTrackUtil2.getSaleTrack().getRecordId()!=null){
 	 						Record record=recordService.findById(saleTrackUtil2.getSaleTrack().getRecordId());
 	 						if(record!=null){
 	 							saleTrackUtils.get(i).setRecord(record.getRecordName());
 	 						}
 	 					}
 	 					 if(saleTrackUtil2.getSaleTrack().getStaffId()!=null){
 	 						 Staff staff1=staffSerivce.findById(saleTrackUtil2.getSaleTrack().getStaffId());
 	 						 if(staff1!=null){
 	 							 saleTrackUtils.get(i).setStaff(staff1.getStaff_name()); 
 	 						 }
 	 					 }
 	 					if(saleTrackUtil2.getSaleTrack().getSaletrackDate()!=null){
 	 						String sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(saleTrackUtil2.getSaleTrack().getSaletrackDate());
 	 						if(sdf!=null){
 	 							saleTrackUtils.get(i).setDate(sdf);
 	 						}
 	 					}
 	 					if(saleTrackUtil2.getSaleTrack().getClientId()!=null){
 	 						Client client=clientService.findById(saleTrackUtil2.getSaleTrack().getClientId());
 	 						if(client!=null){
 	 							saleTrackUtils.get(i).setClient(client.getClient_name());
 	 						}
 	 					}
 	 					if(saleTrackUtil2.getSaleTrack().getSaleId()!=null){
 	 						Sale sale=saleService.findByID(saleTrackUtil2.getSaleTrack().getSaleId());
 	 						if(sale!=null){
 	 							saleTrackUtils.get(i).setSale(sale.getSaleOpportunityName());
 	 						}
 	 					}
 	 					
 	 					i++;
 	 				}	
 	 					}
 	 				}
				}
 			   
 		}
		return saleTrackUtils;
	}
	
}
